package com.suli.account.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.suli.account.entity.Ridership;
import com.suli.account.entity.RidershipVO;
import com.suli.account.mapper.RidershipMapper;
import com.suli.account.service.RidershipService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author SuLi
 * @since 2022-04-16
 */
@Service
@Transactional
public class RidershipServiceImpl extends ServiceImpl<RidershipMapper, Ridership> implements RidershipService {

    @Autowired
    RidershipMapper ridershipMapper;

    @Override
    public Map<String, Object> getRidership(Date startTime, Date endTime) {
        QueryWrapper<Ridership> queryWrapper=new QueryWrapper<>();
        queryWrapper.between("enter_date", startTime, endTime);
        List<Ridership> riderships = ridershipMapper.selectList(queryWrapper);
        List<Ridership> list=new ArrayList<>();
        for (int i = 0; i < riderships.size(); i++) {
            RidershipVO ridershipVO=new RidershipVO();
            ridershipVO.setEnterDate(riderships.get(i).getEnterDate());
            ridershipVO.setQuality(riderships.get(i).getQuality());
            if(i!=0){
               double d= (riderships.get(i).getQuality().doubleValue()-riderships.get(i-1).getQuality().doubleValue())/riderships.get(i-1).getQuality().doubleValue();
               d=(double) Math.round(d*100)/100;
               ridershipVO.setRate(d);
            }else {
                ridershipVO.setRate(0.00);
            }
            list.add(ridershipVO);
        }
        Map<String, Object> map=new HashMap<>();
        map.put("ridership", list);
        return map;
    }
}
